/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.bean;
 
import java.util.List; 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import toesp.entity.Pengguna;

/**
 *
 * @author satria
 */
public class PenggunaBean {
    private EntityManagerFactory emf;

    public PenggunaBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public boolean add(Pengguna pg) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null ;
        
        try { 
            EntityManager em = getEntityManager();
            if(this.getOnebyId(pg.getPenggUid())==null){
                em.getTransaction().begin();
                em.persist(pg);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) { 
            tx.rollback(); 
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public boolean edit(Pengguna pg) throws IllegalStateException, SystemException{
        Transaction tx = null;
        boolean bolRet = false; 
        
        try {
            EntityManager em = getEntityManager();
            if(this.getOnebyId(pg.getPenggUid())!=null){
                em.getTransaction().begin();
                em.merge(pg);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public boolean del(String p_strNpm) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null; 
        
        try {
            EntityManager em = getEntityManager();
            Pengguna pg = this.getOnebyId(p_strNpm);
            if(pg!=null){
                em.getTransaction().begin();
                em.remove(pg);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public List<Pengguna> getAll() throws Exception { 
        List<Pengguna> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Pengguna o ORDER BY o.penggNamalengkap ASC");     
            lstPg = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstPg;
    }
    
    public Pengguna getOnebyId(String strPgId) throws Exception {
        Pengguna p = null;
         
        try {
            EntityManager em = getEntityManager();
            Query  q = em.createQuery("SELECT o FROM Pengguna o WHERE o.penggUid=:p_penggUid");   
            q.setParameter("p_penggUid", strPgId.trim());            
            List<Pengguna> lstPg = q.getResultList();
            p = (Pengguna) lstPg.get(0); 
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return p;
    }
    
    public int count() {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT COUNT(o) FROM Pengguna o");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    public List<Pengguna> getPaging(int pintStart, int pintMax) throws Exception {
        List<Pengguna> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Pengguna o ORDER BY o.penggNamalengkap ASC");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    public List<Pengguna> getFindByNamePaging(String strFind,int pintStart, int pintMax) throws Exception {
        List<Pengguna> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Pengguna o WHERE o.penggNamalengkap LIKE :p_strNama ORDER BY o.penggNamalengkap ASC");
            q.setParameter("p_strNama","%"+strFind+"%");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
//    public Pengguna getOnebyId(String p_strPgg) throws Exception {
//        Pengguna o = null;
//        try {              
//            EntityManager em = getEntityManager();
//            Query q = em.createQuery("SELECT o FROM Pengguna o WHERE o.penggUid=:p_strPgg");   
//            q.setParameter("p_strPgg", p_strPgg);            
//            List<Pengguna> lstPg = q.getResultList();
//            o = (Pengguna) lstPg.get(0);
//        } catch (Exception e) {            
//        }finally{   
//            //this.getCloseEntityManager();
//        }  
//        return m;
//    }  
    public void commitaj(){
        
    }
}
